using System;

using System.Windows.Forms;

using System.Drawing;

public class calculator : Form
{

    Button[] b = new Button[10];

    Button bDot, bPlus, bSub, bMul, bDiv, bEqu, bClr, bNeg, bSqrt, b1_x, bMc, bMs, bMr, bMp, txtMemo;

    Panel panCalc;

    TextBox txtCalc;

    Double dblAcc;

    Double dblSec;

    Double dbMemo;

    bool blnMemo, blnFrtMemo;

    bool blnClear, blnFrstOpen;

    String strOper;

    public calculator()
    {

        try
        {

            this.Text = "Calculator";

            panCalc = new Panel();

            txtCalc = new TextBox();

            txtCalc.BackColor = Color.White;

            txtCalc.Location = new Point(10, 10);

            txtCalc.Size = new Size(265, 20);

            txtCalc.ReadOnly = true;

            txtCalc.RightToLeft = RightToLeft.Yes;

            panCalc.Size = new Size(290, 270);

            panCalc.BackColor = Color.Beige;

            panCalc.Controls.Add(txtCalc);

            addButtons(panCalc);

            this.Size = new Size(290, 270);

            this.Controls.Add(panCalc);

            dblAcc = 0;

            dblSec = 0;

            dbMemo = 0;

            blnMemo = false;

            blnFrtMemo = false;

            blnFrstOpen = true;

            blnClear = false;

            strOper = new String('=', 1);

        }

        catch (Exception e)
        {

            Console.WriteLine("error ...... " + e.StackTrace);

        }

    }

    private void addButtons(Panel p)
    {

        for (int i = 0; i <= 9; i++)
        {

            b[i] = new Button();

            b[i].Text = Convert.ToString(i);

            b[i].Size = new Size(30, 30);

            b[i].BackColor = Color.White;

            b[i].ForeColor = Color.Blue;

            b[i].Click += new EventHandler(btn_clk);

            p.Controls.Add(b[i]);

        }

        b[0].Location = new Point(10, 160);

        b[1].Location = new Point(10, 120);

        b[4].Location = new Point(10, 80);

        b[7].Location = new Point(10, 40);



        b[2].Location = new Point(50, 120);

        b[5].Location = new Point(50, 80);

        b[8].Location = new Point(50, 40);



        b[3].Location = new Point(90, 120);

        b[6].Location = new Point(90, 80);

        b[9].Location = new Point(90, 40);



        bDot = new Button();
        bDot.Size = new Size(30, 30);
        bDot.Location = new Point(50, 160);
        bDot.BackColor = Color.White;
        bDot.ForeColor = Color.Blue;
        bDot.Text = ".";
        bDot.Click += new EventHandler(btn_clk);

        bPlus = new Button();
        bPlus.Size = new Size(30, 30);
        bPlus.Location = new Point(130, 160);
        bPlus.BackColor = Color.White;
        bPlus.ForeColor = Color.OrangeRed;
        bPlus.Text = "+";
        bPlus.Click += new EventHandler(btn_Oper);

        bSub = new Button();
        bSub.Size = new Size(30, 30);
        bSub.Location = new Point(130, 120);
        bSub.BackColor = Color.White;
        bSub.ForeColor = Color.OrangeRed;
        bSub.Text = "-";
        bSub.Click += new EventHandler(btn_Oper);

        bMul = new Button();
        bMul.Size = new Size(30, 30);
        bMul.Location = new Point(130, 80);
        bMul.BackColor = Color.White;
        bMul.ForeColor = Color.OrangeRed;
        bMul.Text = "*";
        bMul.Click += new EventHandler(btn_Oper);

        bDiv = new Button();
        bDiv.Size = new Size(30, 30);
        bDiv.Location = new Point(130, 40);
        bDiv.BackColor = Color.White;
        bDiv.ForeColor = Color.OrangeRed;
        bDiv.Text = "/";
        bDiv.Click += new EventHandler(btn_Oper);

        bEqu = new Button();
        bEqu.Size = new Size(30, 30);
        bEqu.Location = new Point(90, 160);
        bEqu.BackColor = Color.White;
        bEqu.ForeColor = Color.OrangeRed;
        bEqu.Text = "=";
        bEqu.Click += new EventHandler(btn_equ);

        bClr = new Button();
        bClr.Size = new Size(45, 30);
        bClr.Location = new Point(170, 40);
        bClr.BackColor = Color.White;
        bClr.ForeColor=Color.OrangeRed;
        bClr.Text = "AC";
        bClr.Click += new EventHandler(btn_clr);

        bNeg = new Button();
        bNeg.Size = new Size(45, 30);
        bNeg.Location = new Point(170, 80);
        bNeg.BackColor = Color.White;
        bNeg.ForeColor = Color.Blue;
        bNeg.Text = "+/-";
        bNeg.Click += new EventHandler(btn_math);

        bSqrt = new Button();
        bSqrt.Size = new Size(45, 30);
        bSqrt.Location = new Point(170, 120);
        bSqrt.BackColor = Color.White;
        bSqrt.ForeColor = Color.Blue;
        bSqrt.Text = "sqrt";
        bSqrt.Click += new EventHandler(btn_math);

        b1_x = new Button();
        b1_x.Size = new Size(45, 30);
        b1_x.Location = new Point(170, 160);
        b1_x.BackColor = Color.White;
        b1_x.ForeColor = Color.Blue;
        b1_x.Text = "1/x";
        b1_x.Click += new EventHandler(btn_math);

        bMc = new Button();
        bMc.Size = new Size(45, 25);
        bMc.Location = new Point(230, 40);
        bMc.BackColor = Color.White;
        bMc.ForeColor = Color.OrangeRed;
        bMc.Text = "MC";
        bMc.Click += new EventHandler(btn_memo);

        bMr = new Button();
        bMr.Size = new Size(45, 30);
        bMr.Location = new Point(230, 80);
        bMr.BackColor = Color.White;
        bMr.ForeColor = Color.OrangeRed;
        bMr.Text = "MR";
        bMr.Click += new EventHandler(btn_memo);

        bMs = new Button();
        bMs.Size = new Size(45, 30);
        bMs.Location = new Point(230, 120);
        bMs.BackColor = Color.White;
        bMs.ForeColor = Color.OrangeRed;
        bMs.Text = "MS";
        bMs.Click += new EventHandler(btn_memo);

        bMp = new Button();
        bMp.Size = new Size(45, 30);
        bMp.Location = new Point(230, 160);
        bMp.BackColor = Color.White;
        bMp.ForeColor = Color.OrangeRed;
        bMp.Text = "M+";
        bMp.Click += new EventHandler(btn_memo);

        txtMemo = new Button();
        txtMemo.Enabled = false;
        txtMemo.Size = new Size(45, 30);
        txtMemo.Location = new Point(230, 200);
        txtMemo.BackColor = Color.Beige;
        txtMemo.ForeColor = Color.Black;
        

        p.Controls.Add(bDot);

        p.Controls.Add(bPlus);

        p.Controls.Add(bSub);

        p.Controls.Add(bMul);

        p.Controls.Add(bDiv);

        p.Controls.Add(bEqu);

        p.Controls.Add(bClr);

        p.Controls.Add(bNeg);

        p.Controls.Add(bSqrt);

        p.Controls.Add(b1_x);

        p.Controls.Add(bMc);

        p.Controls.Add(bMr);

        p.Controls.Add(bMs);

        p.Controls.Add(bMp);

        p.Controls.Add(txtMemo);
    }

    private void btn_clk(object obj, EventArgs ea)
    {
        if (blnClear)

            txtCalc.Text = "";

        Button b3 = (Button)obj;

        txtCalc.Text += b3.Text;

        if (txtCalc.Text == ".")
            txtCalc.Text = "0.";
        
       dblSec = Convert.ToDouble(txtCalc.Text);
       blnClear = false;

    }

    private void btn_memo(object obj, EventArgs ea)
    {
        Button btmp = (Button)obj;

        if (btmp.Text == "MS" && txtCalc.Text.Length>0)
        {
            dbMemo = Convert.ToDouble(txtCalc.Text);
            blnMemo = true;
            blnFrtMemo = true;
            txtMemo.Text = "M";
        }
        if (btmp.Text == "MR" && blnMemo== true )
        {   
            txtCalc.Text = Convert.ToString(dbMemo);
            dblSec = dbMemo;
        }
        if (btmp.Text == "MC" && blnMemo == true)
        {
            blnMemo = false;
            txtMemo.Text = "";
        }
        if (btmp.Text == "M+")
           if(blnFrtMemo)
                dbMemo =dbMemo+ Convert.ToDouble(txtCalc.Text);
           if (blnFrtMemo == false && txtCalc.Text.Length>0)
           {
               dbMemo = Convert.ToDouble(txtCalc.Text);
               blnMemo = true;
               blnFrtMemo = true;
               txtMemo.Text = "M";
           }
        
        blnClear = true;


    }

    private void btn_math(object obj, EventArgs ea)
    {
        Button btmp = (Button)obj;

        if (blnFrstOpen)

            if(btmp.Text=="1/x")

                dblAcc = 1 / dblSec;

            if (btmp.Text == "sqrt")

                dblAcc = Math.Sqrt(dblSec);

            if (btmp.Text == "+/-")

                dblAcc = -1 * dblSec;
        
        txtCalc.Text = Convert.ToString(dblAcc);

        dblSec = dblAcc;               
     }


    private void btn_Oper(object obj, EventArgs ea)
    {

        Button tmp = (Button)obj;

        strOper = tmp.Text;

        if (blnFrstOpen)

            dblAcc = dblSec;

        else

            calc();

        blnFrstOpen = false;

        blnClear = true;

    }


    private void btn_clr(object obj, EventArgs ea)
    {

        clear();

    }


    private void btn_equ(object obj, EventArgs ea)
    {

        calc();

    }

    private void calc()
    {
        switch (strOper)
        {

            case "+":

                dblAcc += dblSec;

                break;

            case "-":

                dblAcc -= dblSec;

                break;

            case "*":

                dblAcc *= dblSec;

                break;

            case "/":

                dblAcc /= dblSec;

            break;
   
        }

        strOper = "=";

        blnFrstOpen = true;

        txtCalc.Text = Convert.ToString(dblAcc);

        dblSec = dblAcc;

        blnClear = true;

    }



    private void clear()
    {

        dblAcc = 0;

        dblSec = 0;

        blnFrstOpen = true;

        txtCalc.Text = "";

        txtCalc.Focus();

    }
   

    private void InitializeComponent()
    {
        this.SuspendLayout();
        // 
        // calculator
        // 
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Name = "calculator";
        this.Load += new System.EventHandler(this.win_Load);
        this.ResumeLayout(false);

    }

    private void win_Load(object sender, EventArgs e)
    {

    }

}
